package cn.clq.basic.util;

import cn.clq.org.domain.Shop;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: 发送邮件的工具类
 * @date 2022/6/13 23:34
 */
public class MailUtils {
    //简单邮件
    public static void  sendSimpleMail(JavaMailSender javaMailSender,String toMail, Shop shop){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1272352079@qq.com");
        //邮件主题
        mailMessage.setSubject("宠物乐园-店铺入驻激活邮件");
        //邮件内容
        mailMessage.setText("亲，您的店铺入驻信息已经通过审核，请尽快完成激活并使用：http://localhost:8080/shop/activation/"+shop.getId());
        //收件人
        mailMessage.setTo(toMail);

        javaMailSender.send(mailMessage);
    }

    //复杂邮件
    public static void sendComplexMail(JavaMailSender javaMailSender, String fromMail, String subject, String text, Boolean bool, Map<String, String> fies, String toMail) throws Exception{
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom(fromMail);
        helper.setSubject(subject);
        //第二个参数 true 代表是否是一个html内容
        helper.setText(text,bool);
        //添加附件
        // 先获取map集合的所有键的set集合，即为map中所有key值的集合
        Set<String> keySet = fies.keySet();
        // 有了set集合，就可以获取其迭代器
        Iterator<String> it = keySet.iterator();
        while (it.hasNext()) {
            String key = it.next();
            // 有了键可以通过map集合的get方法获取其对应的值
            String value = fies.get(key);
            // 获得key和value值
            helper.addAttachment(key,new File(value));
        }
        //收件人
        helper.setTo(toMail);
        //发送邮件
        javaMailSender.send(mimeMessage);
    }

}
